package top.dancun.easstudent.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import top.dancun.common.utils.DataUtils;
import top.dancun.easstudent.entity.DormitoryArrangementEntity;
import top.dancun.easstudent.service.DormitoryArrangementService;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easstudent.service.StudentService;


/**
 * 宿舍安排
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-11-05 10:15:04
 */
@RestController
@RequestMapping("/dormitoryarrangement")
public class DormitoryArrangementController extends AbstractController{
    @Autowired
    private DormitoryArrangementService dormitoryArrangementService;
    @Autowired
    private StudentService studentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easstudent:da:list")
    public R list(@RequestBody Map<String, Object> params){
        Map<String ,String> data= DataUtils.mapAddNoEmpty(params,DormitoryArrangementEntity.class);
        PageUtils page = dormitoryArrangementService.queryPage(params,data);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("easstudent:da:info")
    public R info(@PathVariable("id") Long id){
		DormitoryArrangementEntity dormitoryArrangement = dormitoryArrangementService.getById(id);
        dormitoryArrangement.setName(studentService.getById(dormitoryArrangement.getId()).getName());
        return R.ok().put("dormitoryArrangement", dormitoryArrangement);
    }

    /**
     * 学生查询宿舍
     */
    @RequestMapping("/getmyda")
    @RequiresPermissions("easstudent:da:info")
    public R getMyDormitoryArrangement(){
        DormitoryArrangementEntity dormitoryArrangement = dormitoryArrangementService.getById(getUserId());
        dormitoryArrangement.setName(studentService.getById(dormitoryArrangement.getId()).getName());
        return R.ok().put("dormitoryArrangement", dormitoryArrangement);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easstudent:da:save")
    public R save(@RequestBody DormitoryArrangementEntity dormitoryArrangement){
		dormitoryArrangementService.save(dormitoryArrangement);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easstudent:da:update")
    public R update(@RequestBody DormitoryArrangementEntity dormitoryArrangement){
		dormitoryArrangementService.updateById(dormitoryArrangement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easstudent:da:delete")
    public R delete(@RequestBody Long[] ids){
		dormitoryArrangementService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/clearroom")
    @RequiresPermissions("easstudent:da:delete")
    public R clearroom(@RequestBody Long roomId){
        dormitoryArrangementService.remove(Wrappers.lambdaUpdate(DormitoryArrangementEntity.class).eq(DormitoryArrangementEntity::getRoomId,roomId));

        return R.ok();
    }

}
